////
////  AttLabel.swift
////  YueChat
////
////  Created by IMac on 2024/9/21.
////
//
//import UIKit
//import CoreText
//
//class AttLabel: UILabel {
//    
//    protocol AttributedLabelDelegate: AnyObject {
//        func didSelectPhoneNumber(_ phoneNumber: String)
//        func didSelectUrl(_ url: String)
//    }
//
//
//    private var stringArray = [String]()
//    private var rangeArray = [NSValue]()
//    private var isRight = false
//    var delegate = AttributedLabelDelegate.self
//
//    override var attributedText: NSAttributedString? {
//        didSet {
//            self.text = attributedText?.string
//            self.isUserInteractionEnabled = true
//        }
//    }
//
//    func setText(_ text: String) {
//        attributedText = subStr(text: text)
//    }
//
//    func setText(_ text: String, isRight: Bool) {
//        self.isRight = isRight
//        setText(text)
//    }
//
//    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        guard let touch = touches.first else { return }
//        let point = touch.location(in: self)
//        let index = characterIndex(atPoint: point)
//        for (i, rangeValue) in rangeArray.enumerated() {
//            let range = rangeValue.rangeValue
//            if range.location <= index && (range.location + range.length) >= index {
//                let str = stringArray[i]
//                let pattern = "[0-9]{5,12}"
//                let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
//                let isNumber = pred.evaluate(with: str)
//                
//                if isNumber {
//        
////                    attributedLabel(<#T##TTTAttributedLabel#>, didSelectLinkWithPhoneNumber: <#T##String#>)
//                } else {
// 
//                    didSelectUrl(str)
//                }
//            }
//        }
//        super.touchesBegan(touches, with: event)
//    }
//    
//
//    private func subStr(text: String) -> NSMutableAttributedString {
//        let regulaStr = "(https|http|ftp|rtsp|mms)://[^\\s]+([a-zA-z0-9])|(([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/?[a-zA-Z0-9\\.&%_\\./-~-]*)?|((http|ftp|https)://[a-zA-Z0-9\\.\\-]+\\.[a-zA-Z]{2,4})(:\\d+)?(/?[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?|(www\\.[a-zA-Z0-9\\.\\-]+[\\.|\\/]([a-zA-Z]{1,4})(:\\d+)?(/?[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"
//        let phoneNumberRegex = "[0-9]{5,11}"
//        let error = NSError(domain: "", code: 0, userInfo: nil)
//
//        let regex = try! NSRegularExpression(pattern: regulaStr, options: [])
//        let arrayOfAllMatches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count))
//
//        stringArray = []
//        rangeArray = []
//
//        for match in arrayOfAllMatches {
//            let substringForMatch = text[Range(match.range, in: text)!]
//            stringArray.append(String(substringForMatch))
//        }
//
//        let regexPhone = try! NSRegularExpression(pattern: phoneNumberRegex, options: [])
//        let arrayOfPhoneMatches = regexPhone.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count))
//
//        for match in arrayOfPhoneMatches {
//            let substringForMatch = text[Range(match.range, in: text)!]
//            stringArray.append(String(substringForMatch))
//        }
//
//        let attributedText = NSMutableAttributedString(string: text, attributes: [NSAttributedString.Key.font: self.font as Any])
//
//        let attributedDict = [
//            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16),
//            NSAttributedString.Key.foregroundColor: isRight ? UIColor.white : UIColor.blue,
//            NSAttributedString.Key.underlineStyle: NSUnderlineStyle.none.rawValue
//        ] as [NSAttributedString.Key : Any]
//
//        for rangeValue in rangeArray {
//            let range = rangeValue.rangeValue
//            attributedText.addAttributes(attributedDict, range: range)
//        }
//
//        return attributedText
//    }
//
//    private func rangesOfString(_ searchString: String, inString str: String) -> NSValue {
//        let searchRange = NSRange(location: 0, length: str.utf16.count)
//        let range = str.range(of: searchString, options: [], range: searchRange)
//        return NSValue(range: range ?? NSRange(location: NSNotFound, length: 0))
//    }
//
//    private func characterIndex(atPoint point: CGPoint) -> CFIndex {
//        // Core Text 相关的实现
//        // ...
//        return NSNotFound
//    }
//
//    private var textRect: CGRect {
//        var rect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
//        rect.origin.y = (bounds.height - rect.height) / 2
//
//        switch textAlignment {
//        case .center:
//            rect.origin.x = (bounds.width - rect.width) / 2
//        case .right:
//            rect.origin.x = bounds.width - rect.width
//        default:
//            break
//        }
//
//        return rect
//    }
//}
//
